public class 分割等和子集 {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        int n = nums.length;
        for (int num : nums) sum += num;
        int m = sum / 2;
        if ((sum & 1) == 1) return false;
        boolean[] dp = new boolean[m + 1];
        dp[0] = true;
        for (int i = 1;i <= n;i++) {
            for (int j = m;j >= nums[i - 1];j--) {
                dp[j] |= dp[j - nums[i - 1]];
            }
        }
        return dp[m];
    }
}
